<?php

namespace app\shopapi\controller;

use app\admin\model\Shop;
use app\common\controller\Shopapi;
use app\common\model\User;
use think\Db;
use think\Request;

/**
 * 店铺订单接口
 */
class Order extends Shopapi
{
    protected $noNeedRight = '*';

    protected $shop_id;

    protected $shop_name;

    protected $shop_info;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        /*$shop_info = \app\admin\model\Shop::alias('s')
            ->join('shop_category sc', 'sc.id=s.category_id', 'LEFT')
            ->where(['s.user_id'=>$this->auth->id])
            ->field('s.*,sc.name as category_name')->find();

        if (empty($shop_info)){
            $this->error('当前用户不是店铺管理员');
        }*/
        $shop_info = Shop::get(['username'=>$this->shopauth->username]);
        if (empty($shop_info)){
            $this->error('当前用户不是店铺管理员');
        }
        $this->shop_info = $shop_info;
        $this->shop_id = $shop_info->id;
        $this->shop_name = $shop_info->shop_name;
    }

    //订单列表
    public function lists()
    {
        $where = ['o.shop_id'=>$this->shop_id,'o.pay_status'=>1];
        $order_status = input('order_status','all');
        if ($order_status!='all'){
            if ($order_status=='pay'){
                //已付款
                $where['o.order_status'] = 1;
            } elseif ($order_status=='apply'){
                //申请退款
                $where['o.order_status'] = 2;
                //$where['o.refund_status'] = 1;
            } elseif ($order_status=='pass'){
                //同意退款
                $where['o.order_status'] = 3;
                //$where['o.refund_status'] = 2;
            } elseif ($order_status=='refuse'){
                //拒绝退款
                $where['o.order_status'] = 4;
                //$where['o.refund_status'] = 3;
            }
        }
        $list = \app\api\model\Order::alias('o')
            ->join('user u', 'u.id=o.user_id', 'LEFT')
            ->field('o.*,u.avatar,u.nickname,u.mobile')
            ->where($where)
            ->order('o.createtime desc')
            ->paginate(10);
        foreach ($list as $k=>$v){
            $list[$k]['mobile'] = maskPhoneNumberRegex($v['mobile']);
        }
        $this->success('',$list);

    }

    //订单详情
    public function detail()
    {
        $id = input('id',0);

        $detail = \app\api\model\Order::alias('o')
            ->join('user u', 'u.id=o.user_id', 'LEFT')
            ->where(['o.shop_id'=>$this->shop_id,'o.id'=>$id])
            ->field('o.*,u.avatar,u.nickname,u.mobile')
            ->find();
        if (!$detail){
            $this->error('订单不存在');
        }
        $detail['mobile'] = maskPhoneNumberRegex($detail['mobile']);
        $this->success('',$detail);
    }

    //同意退款
    public function refundPass()
    {
        $id = input('id',0);
        $order = \app\api\model\Order::get($id);
        if ($order->refund_status==0){
            $this->error('该订单未申请售后');
        }
        if ($order->refund_status!=1){
            $this->error('该订单售后已处理');
        }
        Db::startTrans();
        try {
            //$order_model = new \app\api\model\Order();
            //$res = $order_model->backrefund($order->id,$order->refund_money);
            $order->order_status = 3;
            $order->order_status_name = '同意退款';
            $order->refund_status = 2;
            $order->refund_action_time = time();
            $order->save();
            User::money($order->refund_money,$order->user_id,'订单退款',$order->id,$order->shop_id);
            \app\common\model\Shop::money(-$order->pay_money,$order->shop_id,'订单退款',$order->id,$order->user_id);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('成功');
    }


    //拒绝退款
    public function refundRefuse()
    {
        $id = input('id',0);
        $refuse_reason = input('refuse_reason','');
        $order = \app\api\model\Order::get($id);
        if ($order->refund_status==0){
            $this->error('该订单未申请售后');
        }
        if ($order->refund_status!=1){
            $this->error('该订单售后已处理');
        }
        $order->refuse_reason = $refuse_reason;
        $order->refund_status = 3;
        $order->refund_action_time = time();
        $order->order_status = 4;
        $order->order_status_name = '拒绝退款';
        if ($order->save()){
            $this->success('成功');
        } else {
            $this->error('失败');
        }

    }

}